package com.wk.lib_base.net

import com.wk.lib_base.BuildConfig
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import java.util.concurrent.TimeUnit

object OkhttpClientUtil {

    private lateinit var mClient: OkHttpClient

    fun clientIns(): OkHttpClient {
        if (!this::mClient.isInitialized) {
            mClient = clientBuilder().build()
        }
        return mClient
    }

    private fun clientBuilder(): OkHttpClient.Builder {
        val builder = OkHttpClient.Builder()
            .connectTimeout(10, TimeUnit.SECONDS)
            .callTimeout(10, TimeUnit.SECONDS)
            .addInterceptor(RequestHeaderInterceptor())

        if (BuildConfig.DEBUG) {
            val loggingInterceptor = HttpLoggingInterceptor()
            loggingInterceptor.level = HttpLoggingInterceptor.Level.BODY
            builder.addInterceptor(loggingInterceptor)
        }

        return builder
    }

}